<?php
/* +----------------------------------------------------------------------
// | 	xaoam.com
// |	author:am
// |	gitee:https://gitee.com/xaoam/amAdmin
// |	email:hi@xaoam.com
// |	
// +----------------------------------------------------------------------*/
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Menu extends Base {

    public function initialize(){
        parent::initialize();
		$this->model = new \app\admin\model\MenuM;
    }

    public function index(){
        return $this->fetch();
    }
	public function lists(){
		$data = request()->param();
		$arr =[];
		$list = $this->model->lists(0);
			foreach($list as $key=>$row){
				$arr[$key] = $row;
				if(isset($data['id'])){
						if($row['id'] == $data['id'] || $row['parentId'] == $data['id']){
							$arr[$key]['disabled'] = true; 
						}				
				}else{
						$arr[$key]['opts'] = getTabOpt(['edit','del'],$this->controller,$row['id']);
				}

				if(isset($data['id'])){
					if($row['id'] == $data['id'] || $row['parentId'] == $data['id']){
						$arr[$key]['disabled'] = true; 
					}
				}

			}
		if($list){
		$this->reJson['code'] = 0;
		$this->reJson['data'] = $arr;	
		}else{
			$this->reJson['msg'] = '数据获取失败!';
		}
		return json($this->reJson);	
	}
	public function add(){
		$data = request()->param();
		if(request()->isPost()){
			$res = $this->model->add($data);
			if($res){
				$this->reJson['msg']='增加成功';
				$this->reJson['code']=200;
			}
			return json($this->reJson);			
		}else{
			return $this->fetch();			
		}
	}
	public function edit(){
		$data = request()->param();
		if(!isset($data['id'])){     
			return json($this->reJson);
		}
		if(request()->isPost()){
			$res = $this->model->edit($data);
			if($res){
				$this->reJson['msg']='增加成功';
				$this->reJson['code']=200;
			}
			return json($this->reJson);
		}else{
			$this->assign('d_json',json_encode(Db::name('links')->where('id',$data['id'])->find()));
			return $this->fetch();			
		}

	}
	public function cue(){
		$data = request()->param();
		$res = $this->model->cue($data);
		if($res){
			$this->reJson['code'] = 200;
			$this->reJson['msg']= '操作成功!';
		}
		return json($this->reJson);
	}	
	public function del(){
		$data = request()->param();
		$res = $this->model->find(['parentId'=>$data['id']]);
		if($res){
			$this->reJson['msg']='删除菜单包含子菜单,请删除子菜单';
			$this->reJson['code']=0;			
		}else{
			$res = $this->model->del($data);
			if($res){
				$this->reJson['msg']='删除成功';
				$this->reJson['code']=200;
			}
		}
		return json($this->reJson);
	}
}
